﻿Imports System.Runtime.InteropServices
Imports System.Threading

Public Class Form1
    Private _application As SolidEdgeFramework.Application
    Private WithEvents _applicationEvents As SolidEdgeFramework.DISEApplicationEvents_Event
    Private _guiThread As Thread

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        _application = Marshal.GetActiveObject("SolidEdge.Application")
        _applicationEvents = _application.ApplicationEvents
        _guiThread = Thread.CurrentThread
    End Sub

    Private Sub SafeAddListViewItem(ByVal ctl As ListView, ByVal text As String)
        Dim objItem As ListViewItem

        If ctl.InvokeRequired Then
            ctl.BeginInvoke(New Action(Of ListView, String)(AddressOf SafeAddListViewItem), ctl, text)
        Else
            objItem = ctl.Items.Add(text)
            objItem.EnsureVisible()
        End If
    End Sub

    Private Sub _applicationEvents_AfterActiveDocumentChange(theDocument As Object) Handles _applicationEvents.AfterActiveDocumentChange
        SafeAddListViewItem(lvEvents, "AfterActiveDocumentChange")
    End Sub

    Private Sub _applicationEvents_AfterCommandRun(theCommandID As Integer) Handles _applicationEvents.AfterCommandRun
        SafeAddListViewItem(lvEvents, "AfterCommandRun")
    End Sub

    Private Sub _applicationEvents_AfterDocumentOpen(theDocument As Object) Handles _applicationEvents.AfterDocumentOpen
        SafeAddListViewItem(lvEvents, "AfterDocumentOpen")
    End Sub

    Private Sub _applicationEvents_AfterDocumentPrint(theDocument As Object, hDC As Integer, ByRef ModelToDC As System.Array, ByRef Rect As System.Array) Handles _applicationEvents.AfterDocumentPrint
        SafeAddListViewItem(lvEvents, "AfterDocumentPrint")
    End Sub

    Private Sub _applicationEvents_AfterDocumentSave(theDocument As Object) Handles _applicationEvents.AfterDocumentSave
        SafeAddListViewItem(lvEvents, "AfterDocumentSave")
    End Sub

    Private Sub _applicationEvents_AfterEnvironmentActivate(theEnvironment As Object) Handles _applicationEvents.AfterEnvironmentActivate
        SafeAddListViewItem(lvEvents, "AfterEnvironmentActivate")
    End Sub

    Private Sub _applicationEvents_AfterNewDocumentOpen(theDocument As Object) Handles _applicationEvents.AfterNewDocumentOpen
        SafeAddListViewItem(lvEvents, "AfterNewDocumentOpen")
    End Sub

    Private Sub _applicationEvents_AfterNewWindow(theWindow As Object) Handles _applicationEvents.AfterNewWindow
        SafeAddListViewItem(lvEvents, "AfterNewWindow")
    End Sub

    Private Sub _applicationEvents_AfterWindowActivate(theWindow As Object) Handles _applicationEvents.AfterWindowActivate
        SafeAddListViewItem(lvEvents, "AfterWindowActivate")
    End Sub

    Private Sub _applicationEvents_BeforeCommandRun(theCommandID As Integer) Handles _applicationEvents.BeforeCommandRun
        SafeAddListViewItem(lvEvents, "BeforeCommandRun")
    End Sub

    Private Sub _applicationEvents_BeforeDocumentClose(theDocument As Object) Handles _applicationEvents.BeforeDocumentClose
        SafeAddListViewItem(lvEvents, "BeforeDocumentClose")
    End Sub

    Private Sub _applicationEvents_BeforeDocumentPrint(theDocument As Object, hDC As Integer, ByRef ModelToDC As System.Array, ByRef Rect As System.Array) Handles _applicationEvents.BeforeDocumentPrint
        SafeAddListViewItem(lvEvents, "BeforeDocumentPrint")
    End Sub

    Private Sub _applicationEvents_BeforeDocumentSave(theDocument As Object) Handles _applicationEvents.BeforeDocumentSave
        SafeAddListViewItem(lvEvents, "BeforeDocumentSave")
    End Sub

    Private Sub _applicationEvents_BeforeEnvironmentDeactivate(theEnvironment As Object) Handles _applicationEvents.BeforeEnvironmentDeactivate
        SafeAddListViewItem(lvEvents, "BeforeEnvironmentDeactivate")
    End Sub

    Private Sub _applicationEvents_BeforeQuit() Handles _applicationEvents.BeforeQuit
        SafeAddListViewItem(lvEvents, "BeforeQuit")
    End Sub

    Private Sub _applicationEvents_BeforeWindowDeactivate(theWindow As Object) Handles _applicationEvents.BeforeWindowDeactivate
        SafeAddListViewItem(lvEvents, "BeforeWindowDeactivate")
    End Sub
End Class
